﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.Windows.Browser;
using System.Net;
using System.Windows.Media.Imaging;

namespace GestaltDesigner
{
    public partial class PreviewPanel : UserControl
    {
        public PreviewPanel()
        {
            InitializeComponent();
        }
        void PreviewPanel_MediaOrimageFailed(object sender, ExceptionRoutedEventArgs e)
        {
            //swallow for now
        }

        public void GeneratePreview(string xaml)
        {
            this.LayoutRoot.Children.Clear();

            FrameworkElement element = XamlReader.Load(xaml) as FrameworkElement;
            this.LayoutRoot.Children.Add(element);
            foreach (FrameworkElement control in ((Canvas)element).Children)
            {
                if (control is Image)
                    ((Image)control).ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(PreviewPanel_MediaOrimageFailed);
                if (control is MediaElement)
                    ((MediaElement)control).MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(PreviewPanel_MediaOrimageFailed);
                if (control is MultiScaleImage)
                    ((MultiScaleImage)control).ImageOpenFailed += new EventHandler<ExceptionRoutedEventArgs>(PreviewPanel_MediaOrimageFailed);

            }

        }
    }

    //public static class FrameworkElementExtension
    //{
    //    [SpecialName]
    //    public static object GetBoundMember(FrameworkElement element, string name)
    //    {
    //        object result = element.FindName(name);
    //        if (result == null)
    //        {
    //            return OperationFailed.Value;
    //        }
    //        return result;
    //    }
    //}
}
